<?php

/**
 * uniBoard API Crawler
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-08 03:38:35 +0000 (Wed, 08 Sep 2010) $
 * $Revision: 60 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class api_crawler extends class_api{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore				= NULL;
	
	/**
	 * Crawler ID
	 *
	 * @var integer
	 */
	
	public $crawler_id 			= 0;
	
	/**
	 * Crawler Name
	 *
	 * @var string
	 */
	
	public $crawler_name			= '';
	
	/**
	 * Crawler Agent
	 *
	 * @var string
	 */
	
	public $crawler_agent			= '';
	
	/**
	 * Crawler Last Visit
	 *
	 * @var integer
	 */
	
	public $crawler_last_visit		= 0;
	
	/**
	 * Crawler Style
	 *
	 * @var integer
	 */
	
	public $crawler_style			= 0;
	
	/**
	 * Crawler Language
	 *
	 * @var integer
	 */
	
	public $crawler_language		= 0;
				
	// ------------------------------------------------------------------------

	/**
	 * Initialise Api
	 *
	 * @param object $appCore
	 * @param integer $target_id
	 */
	
	function __construct( $appCore, $target_id = 0){
					
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Store Crawler ID
		$this -> crawler_id 	= (integer) $target_id;
		
		// Autoload If Crawler ID is bigger than zero
		if ( $target_id > 0)
		{
			// Make select
			$appCore -> db -> query( 'SELECT *
			FROM ' . DB_PREFIX . 'crawlers
			WHERE crawler_id = \'' . $this -> crawler_id . '\'
			LIMIT 1');
			
			// Crawler found
			if ( $crawler_result = $appCore -> db -> fetch_assoc())
			{
				$this -> data_loaded				= true;
				
				$this -> crawler_name	 			= $crawler_result['crawler_name'];
				$this -> crawler_agent	 			= $crawler_result['crawler_agent'];
				$this -> crawler_last_visit	 		= $crawler_result['crawler_last_visit'];
				$this -> crawler_style	 			= $crawler_result['crawler_style'];
				$this -> crawler_language 			= $crawler_result['crawler_language'];
			}
			else
			{
				//give action an error
				$this -> setError( $appCore -> lang['acp_crawler_not_found']);
			}
			
			// Free result
			$appCore -> db -> freeResult();
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Save API data to DB
	 *
	 */
	
	function saveData(){
		
		// Do mysql query only when we have changes, and no errors
		if ( !empty( $this -> changes) && !$this -> error)
		{	
			// Insert?
			if ( $this -> crawler_id == 0)
			{
				// Insert new record
				$this -> appCore -> db -> insert( $this -> changes, 'crawlers');
			
				// Store new crawler id
				$this -> crawler_id = $this -> appCore -> db -> insertId();
			}
			else
			{
				// Update record
				$this -> appCore -> db -> update( $this -> changes, 'crawlers', 'crawler_id = \'' . $this -> crawler_id . '\'');
			}
		
			// Flush cache
			class_cache::flushCache( 'crawlers');
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Delete API data to DB
	 *
	 */
	
	function deleteData(){
		
		// Delete data
		$this -> appCore -> db -> delete( 'crawlers', 'crawler_id = \'' . $this -> crawler_id . '\'');
		
		// Flush cache
		class_cache::flushCache( 'crawlers');
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set crawler_name
	 *
	 * @param string $new_value
	 */
	
	function setCrawlerName( $new_value){
		
		if ( isset( $new_value[0]))
		{
			// Store in object
			$this -> crawler_name = $new_value;
			
			// Check max lenght
			if ( mb_strlen( $new_value) > 255)
			{
				// Set error
				$this -> setError( $this -> appCore -> lang['acp_crawler_name_too_long']);
			}
			else
			{
				// Add to made changes
				$this -> changes['crawler_name'] = $this -> appCore -> db -> strEscape( $this -> crawler_name);
			}
		}
		else
		{
			// Set error
			$this -> setError( $this -> appCore -> lang['acp_crawler_name_empty']);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set crawler_agent
	 *
	 * @param string $new_value
	 */
	
	function setCrawlerAgent( $new_value){
		
		if ( isset( $new_value[0]))
		{
			// Store in object
			$this -> crawler_agent = $new_value;
			
			// Check max lenght
			if ( mb_strlen( $new_value) > 255)
			{
				// Set error
				$this -> setError( $this -> appCore -> lang['acp_crawler_agent_too_long']);
			}
			else
			{
				// Add to made changes
				$this -> changes['crawler_agent'] = $this -> appCore -> db -> strEscape( $this -> crawler_agent);
			}
		}
		else
		{
			// Set error
			$this -> setError( $this -> appCore -> lang['acp_crawler_agent_empty']);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set crawler_last_visit
	 *
	 * @param integer $new_value
	 */
	
	function setCrawlerLastVisit( $new_value){
		
		// Store in object
		$this -> crawler_last_visit = $new_value;
		
		// Add to made changes
		$this -> changes['crawler_last_visit'] = (int) $this -> crawler_last_visit;
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set crawler_style
	 *
	 * @param integer $new_value
	 */
	
	function setCrawlerStyle( $new_value){
		
		// Store in object
		$this -> crawler_style = $new_value;
		
		// Add to made changes
		$this -> changes['crawler_style'] = (int) $this -> crawler_style;
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set crawler_language
	 *
	 * @param integer $new_value
	 */
	
	function setCrawlerLanguage( $new_value){
		
		// Store in object
		$this -> crawler_language = $new_value;
		
		// Add to made changes
		$this -> changes['crawler_language'] = (int) $this -> crawler_language;
		
	}
					
}